/**
 * 
 */
package de.cbf.cam.dsa.facet.talent.helper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.cbf.cam.dsa.facet.talent.FacetTalent;
import de.cbf.util.uuidheader.entities.NamedEntity;
import de.cbf.util.uuidheader.entities.UUIDHeader;
import de.cbf.util.uuidheader.exceptions.EntityMergeException;

/**
 * @author salter
 *
 */
public class BeTypeValue extends NamedEntity {
	private static final Log logger = LogFactory.getLog(BeTypeValue.class);
	
	
	public static final String PN_MathType	= "math_type";
	public static final String PN_Value		= "value";
	
	public static final int PI_MathType	= 11;
	public static final int PI_Value	= 12;
	
	public static final Integer	UNSET_MathType	= FacetTalent.MATH_TYPE_ADD;
	public static final Integer	UNSET_Value		= 0;
	
	public static final String TEXT_UNSET_MathType	= "MathType not set";	
	public static final String TEXT_UNSET_Value		= "Value not set";
	/**
	 * +/- or *
	 */
	protected Integer fMathType;
	/**
	 * value for calculation
	 */
	protected Integer fValue;
	
	/**	 */
	public BeTypeValue() {
		super(FacetTalent.BE_SOURCE_DEFAULT);
	}

	/**
	 * @param name
	 */
	public BeTypeValue(String name) {
		super(name);		
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.uuidheader.entities.NamedEntity#init()
	 */
	@Override
	protected void init() {		
		super.init();		
		fMathType 	= UNSET_MathType;
		fValue		= UNSET_Value;
	}	
	
	public Integer calculateBE(Integer currentBE) {
		Integer result = 0;
		switch (fMathType) {
			case FacetTalent.MATH_TYPE_ADD:
				result = fValue + currentBE;
				break;
			case FacetTalent.MATH_TYPE_MULT:
				result = fValue * currentBE;
				break;	
			default:
				logger.error("Unknown mathType: "+fMathType);
				break;
		}
		return result;
	}

	@Override
	protected void doMergeDataFrom(UUIDHeader entity, boolean i_am_older)
			throws EntityMergeException {
		super.doMergeDataFrom(entity, i_am_older);
		if (entity instanceof BeTypeValue) {
			BeTypeValue other = (BeTypeValue) entity;
			if(i_am_older){
				this.fMathType	= other.fMathType;
				this.fValue = other.fValue;
			}
		}else{
			String mergeEntityClass = entity == null? "null":entity.getClass().getName();
			String errorMsg ="EntityToMerge "+mergeEntityClass+" does not fit class:"+this.getClass().getName();
			throw new EntityMergeException(errorMsg);
		}
	}
		
	public Integer getMathType(){
		return fMathType;
	}

	public void setMathType(Integer newMathType) {
		if(newMathType == null || newMathType.equals(fMathType)) return;
		Integer oldMathType = this.fMathType;
		this.fMathType = newMathType;
		propertyChangeSupport.fireIndexedPropertyChange(PN_MathType, PI_MathType,
				oldMathType,this.fMathType);	
	}	 
	
	public Integer getValue(){
		return fValue;
	}

	public void setValue(Integer newValue) {
		if(newValue == null || newValue.equals(fValue)) return;
		Integer oldValue = this.fValue;
		this.fValue = newValue;
		propertyChangeSupport.fireIndexedPropertyChange(PN_Value, PI_Value,
				oldValue,this.fValue);	
	}
}
